<?php
namespace app\admin\logic;
use \PhpOffice\PhpSpreadsheet\Spreadsheet;
use \PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class WorkRecordNoticeLogic extends BaseLogic {

    /**
     * 获取通知公告查询条件
     * @param $department
     * @param $start_time
     * @param $end_time
     * @return array
     */
    public function get_search_wrn_maps($department,$start_time,$end_time){
        $maps = [];

        if($department != '全部'){
            $u_maps = [];
            $u_maps[] = ['department','=',$department];
            $openids = model('user')->get_column($u_maps,'openid');
            $maps[] = ['openid','in',$openids];
        }

        if(!empty($start_time) && !empty($end_time)){
            $maps[] = ['c_time','between',[$start_time,$end_time]];
        }

        return $maps;
    }

    /**
     * 获取通知公告
     * @param $params
     * @return \think\response\Json
     */
    public function get_wrn($params){
        $maps = $this->get_search_wrn_maps($params['department'],$params['start_time'],$params['end_time']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('work_record_notice')->get_list($maps,'id,openid,name,content,pic,c_time','id desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            $list[$k]['pic'] = !empty($v['pic']) ? json_decode($v['pic'],true) : [];

            //查找用户
            $u_maps = [];
            $u_maps[] = ['openid','=',$v['openid']];
            $u_detail = model('user')->get_detail($u_maps,'id,username,department');
            $list[$k]['username'] = $u_detail['username'];
            $list[$k]['department'] = $u_detail['department'];

            //查找抄送
            $c_maps = [];
            $c_maps[] = ['from_openid','=',$v['openid']];
            $c_maps[] = ['sourceid','=',$v['id']];
            $c_maps[] = ['source','=',6];
            $copy_openid_arr = model('copy')->get_column($c_maps,'copy_openid');

            $u_maps = [];
            $u_maps[] = ['openid','in',$copy_openid_arr];
            $copy_username_arr = model('user')->get_column($u_maps,'username');
            $list[$k]['copy_username'] = implode('，',$copy_username_arr);
        }

        $total_num = model('work_record_notice')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 生成通知公告excel
     * @param $ExcelName
     * @param $data
     * @return bool
     */
    public function gen_wrn_excel($ExcelName,$data){
        if(count($data[0]) > 26){
            return $this->get_response(0,'导出excel最大列数为26，请缩减数据列数');
        }
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        for($row = 1;$row <= count($data);$row++){
            $index = 0;
            foreach($data[$row-1] as $k => $v) {
                $col = chr($index + 65);
                $cell = ($col . $row);
                $sheet->setCellValue($cell, $v);
                $index++;
            }
        }

        header('Content-Type: application/vnd.ms-excel;charset=utf8-general-ci');
        header('Content-Disposition: attachment;filename='.$ExcelName);
        header('Cache-Control: max-age=0');

        $writer = new Xlsx($spreadsheet);
        $writer->save('php://output');
        //$writer->save($ExcelName);
        return true;
    }
}